package com.wyl.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.domain.dto.SysRoleDTO;
import com.wyl.template.domain.dto.SysUserDTO;
import com.wyl.template.domain.dto.SysUserRoleDTO;
import com.wyl.template.domain.dto.UserRoleDTO;
import com.wyl.template.domain.model.SysRoleDO;
import com.wyl.template.domain.model.SysUserDO;
import com.wyl.template.domain.model.SysUserRoleDO;
import com.wyl.template.domain.vo.UserRoleVO;
import com.wyl.template.exception.BusinessException;
import com.wyl.template.exception.ErrorCodeEnum;
import com.wyl.template.mapper.SysUserRoleMapper;
import com.wyl.template.service.SysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyl.template.util.ObjConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author wyl
 * @since 2021-06-11
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRoleDO> implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 根据用户，获得角色列表
     * @param userId
     * @return
     */
    @Override
    public List<SysUserRoleDO> listUserRoles(Long userId) {
        List<SysUserRoleDO> userRoleList = lambdaQuery().eq(SysUserRoleDO::getUserId, userId).list();
        return userRoleList;
    }

    /**
     * 查询用户列表的角色
     * @param userList
     * @return
     */
    @Override
    public List<UserRoleVO> listUserRolesIn(List<SysUserDO> userList) {
        if(userList == null || userList.size()<1){
            return null;
        }

        List<Long> ids = new ArrayList<>();
        userList.forEach(item -> {
            ids.add(item.getId());
        });
        List<SysUserRoleDTO> sysUserRoles = userRoleMapper.listUserRolesIn(ids);

        List<UserRoleVO> userRoleVOList = userList.stream().map(user -> {
            List<SysRoleDO> roleList = new ArrayList<>();
            sysUserRoles.forEach(userRole -> {
                if (!ObjectUtils.isEmpty(userRole.getRoleId()) && user.getId().equals(userRole.getUserId())) {
                    SysRoleDO roleDO = new SysRoleDO();
                    roleDO.setId(userRole.getRoleId());
                    roleDO.setRoleName(userRole.getRoleName());
                    roleDO.setDescription(userRole.getDescription());
                    roleList.add(roleDO);
                }
            });
            UserRoleVO userRoleVO = ObjConverter.convert(user, UserRoleVO.class);
            userRoleVO.setRoles(roleList);
            return userRoleVO;
        }).collect(Collectors.toList());

        return userRoleVOList;
    }

    /**
     * 更新用户角色关系
     * @param userId
     * @param list
     * @return
     */
    @Override
    public ResponseResult updateUserRole(Long userId, List<SysUserRoleDO> list) {
        if(userId == null){
            throw new BusinessException(ErrorCodeEnum.UPDATE_FAILURE);
        }
        SysUserRoleDO userRoleDO = new SysUserRoleDO();
        userRoleDO.setUserId(userId);
        QueryWrapper<SysUserRoleDO> queryWrapper = new QueryWrapper<>(userRoleDO);
        // 先删除，再插入
        userRoleMapper.delete(queryWrapper);

        if (!ObjectUtils.isEmpty(list)) {
            if (!saveBatch(list)) {
                throw new BusinessException(ErrorCodeEnum.UPDATE_FAILURE);
            }
        }
        return ResponseResult.success();
    }

}
